package handlers

import (
	"bala-bala-drive/cache"
	"bala-bala-drive/models"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"time"
)

func List(c *gin.Context) {
	var article []models.Article
	redis_key := "list_error_"
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {

		models.DB.Find(&article)
		jsonData, _ := json.Marshal(article)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)

	} else {
		json.Unmarshal([]byte(cacheData), &article)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": article,
	})
	return
}
